package com.hanhanxiaochong.zhongxiangchuanmei.view.main.act;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.hanhanxiaochong.zhongxiangchuanmei.R;
import com.hanhanxiaochong.zhongxiangchuanmei.presenter.RegisterPresenter;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ResourcesUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.util.RxTimerUtil;
import com.hanhanxiaochong.zhongxiangchuanmei.util.ToastUtils;
import com.hanhanxiaochong.zhongxiangchuanmei.view.base.BaseActivity;
import com.hanhanxiaochong.zhongxiangchuanmei.view.iview.IRegisterView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Author : 贾桐
 * E-mail : 653773284@qq.com
 * Time   : 2018/4/16.
 * Desc   : 注册act
 */

public class RegistActivity extends BaseActivity<RegisterPresenter> implements IRegisterView {

    @BindView(R.id.et_phone_number)
    EditText etPhoneNumber;
    @BindView(R.id.txt_yanzheng)
    TextView txtYanzheng;
    @BindView(R.id.et_yanzheng)
    EditText etYanzheng;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.et_invite_code)
    EditText etInviteCode;

    @Override
    protected void initView() {
        setContentView(R.layout.activity_regist);
    }

    @Override
    protected void initPresenter() {
        mPresenter = new RegisterPresenter(this);
    }

    @Override
    protected void init() {

    }

    @OnClick({R.id.txt_login, R.id.txt_yanzheng, R.id.txt_regist, R.id.txt_user_agreement})
    public void onViewClicked(View view) {
        String phoneNumber = etPhoneNumber.getText().toString().trim();
        String verifyCode = etYanzheng.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String inviteCode = etInviteCode.getText().toString().trim();
        switch (view.getId()) {
            case R.id.txt_login:
                startActivity(LoginActivity.createIntent(this));
                finish();
                break;
            case R.id.txt_yanzheng:
                mPresenter.sendMsg(phoneNumber,"1");
                RxTimerUtil.interval(1, 60, new RxTimerUtil.IRxNext() {
                    @Override
                    public void doNext(long number) {
                        txtYanzheng.setClickable(false);
                        txtYanzheng.setText(String.format(ResourcesUtils.getString(R.string.repeat_verify_code_hint),60 - number));
                    }

                    @Override
                    public void doComplete() {
                        txtYanzheng.setClickable(true);
                        txtYanzheng.setText("验证");
                        txtYanzheng.setTextColor(ResourcesUtils.getColor(R.color.pink));
                    }
                });
                break;
            case R.id.txt_regist:
                mPresenter.register(phoneNumber,verifyCode,password,inviteCode);
                break;
            case R.id.txt_user_agreement:
                break;
        }
    }

    @Override
    public void registerSuccess(String data) {
        ToastUtils.showToast(data);
        finish();
    }
}
